/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.backup.user;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author javaking
 */
public class Login {
Connect c = new Connect();
    //taking an objecs from  prepared statement
    PreparedStatement sql_select_Login,sql_insert_Login;
    ResultSet resultset;
    public Login(){
        try {
            sql_select_Login = c.connect.prepareStatement("select * from Login where user_email=?");
            sql_insert_Login = c.connect.prepareStatement("insert into Login (user_email,user_password,user_name,user_os_type,user_pc_name,user_edition,os_version) values (?,?,?,?,?,?,?)");
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    public boolean login(String email, String Password){
        try {
            sql_select_Login.setString(1, email);
            resultset = sql_select_Login.executeQuery();
            if(resultset.wasNull())
                return false;
            else
            while(resultset.next()){
                PasswordService ps=new PasswordService();
                if(resultset.getString(3).equals(ps.getMD5Hash(Password))){
                return true;
                }
                else
                    return false;
            }
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return false;
    }
    public boolean regsiter(String name,String email, String Password,String os, String pc_name, String edition,String os_version){
        try {

            sql_insert_Login.setString(1, email);
            sql_insert_Login.setString(2, Password);
            sql_insert_Login.setString(3, name);
            sql_insert_Login.setString(4, os);
            sql_insert_Login.setString(5, pc_name);
            sql_insert_Login.setString(6, edition);
            sql_insert_Login.setString(7, os_version);


            sql_insert_Login.executeUpdate();
            return true;
        }//end try
        catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }//end catch
    }
    public static void main(String[] args) {
        Login login=new Login();

        System.out.println(login.login("hany@mufic.com", "javaking"));
    }
}
